<?php
class CommentWidget extends Widget {

    public $model;
    public $limit;
    public $viewFile;
    
    /**
     * Initializes the widgets
     */
    public function init() {
        parent::init();
        if($this->viewFile===null) $this->viewFile='_commentWidget';
    }

    /**
     * Execute the widgets
     */
    public function run() {
        Yii::beginProfile(get_class($this));

        $model= new Comment('userCreate');
        $model->module_=get_class($this->model);
        $model->moduleId_=$this->model->id;
        $model->limit_=$this->limit;
        $form=Yii::app()->request->getParam(get_class($model));
        if($form){
            $model->attributes=$form;
            if($model->addRecord()){
                $this->model->updateCommentCounter();
                $model->refresh();
                $this->controller->refresh();
            }
        }
        
        $this->render('componentsViewPath.commentWidget.'.$this->viewFile, array(
            'dataProvider'=>$model->findAllByModuleId(),
            'model'=>$model,
            'data'=>$this->model,
            'user'=>Yii::app()->user,
            'formConfig' => array(
                'id' => get_class($model),
                'enableAjaxValidation' => true,
                'enableClientValidation' => true,
                'clientOptions' => array(
                    'validateOnSubmit' => true,
                ),
                'focus' => array($model, 'message_'),
            ),
        ));
        
        Yii::endProfile(get_class($this));
    }

}//end class